<?php
/*由脚本生成 powerBy--Reer*/

namespace app\common\manage;

use app\common\bean\BaseBean;
use app\common\bean\GlobalBean;
use app\common\bean\ListMap;
use app\common\bean\StoreBean;
use app\common\bean\StoreRoleBean;
use app\common\bean\StoreUserBean;
use app\common\enum\StoreUserStatusEnum;
use app\common\exception\MyException;
use app\common\model\StoreRoleModel;
use app\common\model\StoreUserModel;
use app\common\utils\TimeUtil;
use app\common\validate\StoreUserValidate;

class StoreUserManage extends BaseManage
{
	protected static $_self = null;

    /**
     * @return StoreUserManage
     */
	static function getInstance(){
		return parent::getInstance();
	}

    /**
     * @desc 获取分页数据
     * @param ListMap $listMap
     * @return ListMap $listMap
     * @throws \Exception
     */
    public function getPaginatorList(ListMap $listMap){
        // TODO: Implement getPaginatorList() method.
        $conditionMap = new ListMap();
        if($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap)){
            $conditionMap->setParameter(StoreUserBean::$alias.POINST.StoreUserBean::$createdAt,$timeArray);
        }
        $conditionMap->setParameter(StoreUserBean::$alias.POINST.StoreUserBean::$name,['like','%'.$listMap->getParameter(StoreUserBean::$name).'%']);
        $conditionMap->setParameter(StoreUserBean::$storeRoleId,$listMap->getParameter(StoreUserBean::$storeRoleId));
        $conditionMap->setParameter(StoreUserBean::$alias.POINST.StoreUserBean::$storeId,$listMap->getParameter(GlobalBean::$storeId));
        !$this->getParamBool($listMap,"orderFieldName") ?: $conditionMap->setParameter('order',StoreUserBean::$alias.POINST.$listMap->getParameter('orderFieldName')." ".$listMap->getParameter('orderType'));
        $conditionMap->setField(StoreUserBean::$alias.POINST."*,".StoreRoleBean::$alias.POINST.StoreRoleBean::$name." as rolename");
        $conditionMap->setAlias(StoreUserBean::$alias);
        $conditionMap->setJoin([StoreRoleBean::$tableName,StoreRoleBean::$alias,StoreRoleBean::$alias.POINST.StoreRoleBean::$id,StoreUserBean::$alias.POINST.StoreUserBean::$storeRoleId]);
        $userMap = StoreUserModel::getInstance()->getJoinList($conditionMap);
        $userMap ?: MyException::throwMyException("查询失败");
        return $userMap;
    }

    /**
     * @desc 添加数据
     * @param StoreUserBean $storeUserBean
     * @return int int
     * @throws \Exception
     */
    public function add($storeUserBean){
        // TODO: Implement add() method.
        StoreUserValidate::getInstance()->goCheck(StoreUserValidate::$add,$storeUserBean->getData());
        $storeUserBean->setStatus(StoreUserStatusEnum::NORMAL);
        $storeUserBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $storeUserBean->setPassword(md5($storeUserBean->getPassword()));
        $result = StoreUserModel::getInstance()->addDataGetInsertId($storeUserBean->getData());
        $result ?: MyException::throwMyException("添加失败");
        return $result;
    }

    /**
     * @desc 编辑数据
     * @param StoreUserBean $storeUserBean
     * @return int int
     * @throws \Exception
     */
    public function edit($storeUserBean)
    {
        // TODO: Implement edit() method.
        StoreUserValidate::getInstance()->goCheck(StoreUserValidate::$edit,$storeUserBean->getData());
        $storeUserModel = StoreUserModel::getInstance();
        $result = $this->checkDataPermssion(StoreUserBean::$id,$storeUserBean->getId(),$storeUserModel,StoreUserBean::$storeId,$storeUserBean->getStoreId());
        $storeUserBean->getPassword() ? $storeUserBean->setPassword(md5($storeUserBean->getPassword())) : $storeUserBean->setPassword($result[StoreUserBean::$password]);
        $result = $storeUserModel->updatedData(array(StoreUserBean::$id => $storeUserBean->getId()),$storeUserBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return true;
    }

    /**
     * @desc 删除数据
     * @param StoreUserBean $storeUserBean
     * @return int int
     * @throws \Exception
     */
    public function delete($storeUserBean)
    {
        // TODO: Implement delete() method.
        StoreUserValidate::getInstance()->goCheck(StoreUserValidate::$pkId,$storeUserBean->getData());
        $storeUserModel = StoreUserModel::getInstance();
        $result = $storeUserModel->deleteData(array(StoreUserBean::$id => $storeUserBean->getId(),StoreUserBean::$storeId => $storeUserBean->getStoreId()));
        $result ?: MyException::throwMyException("删除失败");
        return true;
    }

    /**
     * @desc 根据主建获取数据
     * @param StoreUserBean $storeUserBean
     * @return StoreUserBean
     * @throws \Exception
     */
    public function getInfo($storeUserBean)
    {
        // TODO: Implement getInfo() method.
        StoreUserValidate::getInstance()->goCheck(StoreUserValidate::$pkId,$storeUserBean->getData());
        $storeUserModel = StoreUserModel::getInstance();
        $result = $storeUserModel->selectData([StoreUserBean::$id => $storeUserBean->getId(),StoreUserBean::$storeId => $storeUserBean->getStoreId()]);
        $result ?: MyException::throwMyException("无数据");
        $storeUserBean->setData($result);
        return $storeUserBean;
    }
}